<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/15
 * Time: 11:03
 */

namespace app\api\model;

use think\facade\Request;
use think\Model;

class Order extends Model
{

     protected $hidden = ['delete_time','update_time'];
    protected $autoWriteTimestamp = true;

    //读取器，不会有太多的\
    public function getSnapItemsAttr($value)
    {
        if(empty($value)){
            return null;
        }
        return json_decode($value);
    }

    //用户地址的读取器
    public function getSnapAddressAttr($value){
        if(empty($value)){
            return null;
        }
        return json_decode(($value));
    }
    
    public function orderGoods(){

        return $this->hasMany('OrderGoods','order_id','id');
    }

    public function select($uid,$type){
 
      if($type==1){
         $res = self::where('user_id',$uid)->with('orderGoods')->order('create_time', 'desc')->select();
      }else{
           $res = self::where('user_id',$uid)->where('status',$type-2)->with('orderGoods')->order('create_time', 'desc')->select();
      }
     
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

    public function detail($id){
        $res = self::where('id',$id)->with('orderGoods')->find();
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }




    //后台的增删改查
    public function Aselect($pagesize,$pagenum){

        $map =[];
    $keywords = Request::param('query');
    if(!empty($keywords)){
    //条件2
     $map[] = ['out_trade_no','like','%'.$keywords.'%'];
}

      $res = db('order')->field('id,out_trade_no,order_total_price,status,total_count,create_time')->where($map)->page($pagenum,$pagesize)->select();
     
    $count = self::count();

    $info = array(
      'data'=>$res,
      'total'=>$count
      );
      return_json(200,'成功',$info);
    }

   

}